import pyqtgraph as pg
from PyQt5.QtWidgets import QWidget


class WaveGraph(QWidget):
    def __init__(self, samplePoints=2000):
        super().__init__()
        pg.setConfigOptions(antialias=True)
        # pg.setConfigOption('leftButtonPan', False)
        # self.resize(1200, 400)
        self.pw = pg.PlotWidget(self)
        self.pw.resize(1120, 320)
        self.pw.showGrid(x=True, y=True)
        self.samplePoints = samplePoints
        self.curveA = self.pw.plot(pen='y')
        self.curveB = self.pw.plot(pen='g')
        self.pw.setXRange(0, samplePoints*4, padding=0.01)
        self.pw.setYRange(min=-1.5, max=1.5, padding=0.01)
        self.x = [i*4 for i in range(0, samplePoints)]

    def init_args(self):
        self.pw.setXRange(0, self.samplePoints * 4, padding=0.01)
        self.pw.setYRange(min=-1.5, max=1.5, padding=0.01)
        self.x = [i * 4 for i in range(0, self.samplePoints)]

    def handle_data(self, dataA, dataB):
        if len(dataA) <= len(self.x):
            self.curveA.setData(self.x[0:len(dataA)], dataA)
            self.curveB.setData(self.x[0:len(dataB)], dataB)
        else:
            self.pw.setXRange(0, len(dataA) * 4, padding=0.01)
            self.x = [i*4 for i in range(0, len(dataA))]
            self.curveA.setData(self.x[0:len(dataA)], dataA)
            self.curveB.setData(self.x[0:len(dataB)], dataB)
